//
//  HLYiYanCollectionViewLayout.m
//  Various
//
//  Created by HarwordLiu on 16/3/25.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "HLYiYanCollectionViewLayout.h"

@interface HLYiYanCollectionViewLayout ()
{
    CGFloat _viewWeight;
    CGFloat _itemHeight;
    CGFloat _itemWidth;
}

@end


@implementation HLYiYanCollectionViewLayout

// 开始布局
- (void)prepareLayout {
    [super prepareLayout];
    _viewWeight = CGRectGetWidth(self.collectionView.frame);
    _itemHeight = self.itemSize.height;
    _itemWidth= self.itemSize.width;
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, (_viewWeight - _itemHeight) / 2);
}

// 设置ContentSize
- (CGSize)collectionViewContentSize {
    NSInteger cellCount = [self.collectionView numberOfItemsInSection:0];
    return CGSizeMake(cellCount * _itemWidth, CGRectGetHeight(self.collectionView.frame));
}

// 设置屏幕展示cell数量
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSInteger cellCount = [self.collectionView numberOfItemsInSection:0];
    CGFloat centerY = self.collectionView.contentOffset.x + _viewWeight / 2;
    NSInteger index = centerY / _itemWidth;
    NSInteger minIndex = MAX(0, index - 2);
    NSInteger maxIndex = MIN((cellCount - 1), (index + 2));
    NSMutableArray *arrayAtt = [NSMutableArray array];
    for (NSInteger i = minIndex; i < maxIndex; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        [arrayAtt addObject:attributes];
    }
    return arrayAtt;
}

// 需要主动调用

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    /**
     *  放大效果
     */
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attributes.size = self.itemSize;

    CGFloat cX = self.collectionView.contentOffset.x + _viewWeight / 2;
    CGFloat attributesX = _itemWidth * indexPath.row + _itemWidth / 2;
    attributes.zIndex = - ABS(attributesX - cX);
    
    CGFloat delta = cX - attributesX;
    CGFloat ratio =  - delta / (_itemWidth * 2);
    CGFloat scale = 1 - ABS(delta) / (_itemWidth * 8.0) * cos(ratio * M_PI_4) - 0.025;
    attributes.transform = CGAffineTransformMakeScale(scale, scale);
    CGFloat centerX = attributesX;
    attributes.center = CGPointMake(centerX, CGRectGetHeight(self.collectionView.frame) / 2);

    return attributes;
}

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {
    CGFloat index = roundf((proposedContentOffset.x + _viewWeight / 2 - _itemWidth / 2) / _itemWidth);
        proposedContentOffset.x = _itemWidth * index + _itemWidth / 2 - _viewWeight / 2;
    return proposedContentOffset;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return !CGRectEqualToRect(newBounds, self.collectionView.bounds);
}



















@end
